<template>
	<view class="detail-container">
		<view v-if="loading" class="loading">
			<uni-load-more status="loading" :contentText="loadingText"></uni-load-more>
		</view>
		
		<block v-else>
			<view class="type-card">
				<view class="type-icon" :class="getTypeClass(maintenanceDetail.maintenance_type)">
					<uni-icons :type="getTypeIcon(maintenanceDetail.maintenance_type)" size="24" color="#fff"></uni-icons>
				</view>
				<view class="type-info">
					<view class="type-title">{{ maintenanceDetail.maintenance_type }}</view>
					<view class="type-desc">{{ getTypeDesc(maintenanceDetail.maintenance_type) }}</view>
				</view>
			</view>
			
			<view class="detail-card">
				<view class="card-title">设备信息</view>
				
				<view class="info-item">
					<text class="label">设备名称</text>
					<text class="value">{{ maintenanceDetail.device_info?.device_name || '暂无数据' }}</text>
				</view>
				
				<view class="info-item">
					<text class="label">设备编号</text>
					<text class="value">{{ maintenanceDetail.device_info?.device_number || '暂无数据' }}</text>
				</view>
				
				<view class="info-item">
					<text class="label">设备类型</text>
					<text class="value">{{ maintenanceDetail.device_info?.device_type || '暂无数据' }}</text>
				</view>
				
				<view class="info-item">
					<text class="label">品牌型号</text>
					<text class="value">{{ maintenanceDetail.device_info?.brand_model || '暂无数据' }}</text>
				</view>
				
				<view class="info-item">
					<text class="label">位置</text>
					<text class="value">{{ maintenanceDetail.device_info?.location_desc || '暂无数据' }}</text>
				</view>
				
				<view class="info-item">
					<text class="label">设备状态</text>
					<text class="value" :class="getStatusClass(maintenanceDetail.device_info?.status)">
						{{ maintenanceDetail.device_info?.status || '暂无数据' }}
					</text>
				</view>
				
				<view class="info-item" v-if="maintenanceDetail.device_info?.warranty_end_date">
					<text class="label">保修期至</text>
					<text class="value">{{ maintenanceDetail.device_info?.warranty_end_date }}</text>
				</view>
			</view>
			
			<view class="detail-card">
				<view class="card-title">维保信息</view>
				
				<view class="info-item">
					<text class="label">维保内容</text>
					<text class="value">{{ maintenanceDetail.content || '暂无数据' }}</text>
				</view>
				
				<view class="info-item">
					<text class="label">维保日期</text>
					<text class="value">{{ maintenanceDetail.maintenance_date || '暂无数据' }}</text>
				</view>
				
				<view class="info-item" v-if="maintenanceDetail.responsible_person">
					<text class="label">负责人</text>
					<text class="value">{{ maintenanceDetail.responsible_person }}</text>
				</view>
				
				<view class="info-item" v-if="maintenanceDetail.cost">
					<text class="label">费用</text>
					<text class="value">¥{{ maintenanceDetail.cost }}</text>
				</view>
			</view>
			
			<view class="detail-card" v-if="maintenanceDetail.repair_info">
				<view class="card-title">关联报修信息</view>
				
				<view class="info-item">
					<text class="label">报修ID</text>
					<text class="value">{{ maintenanceDetail.repair_info.repair_id }}</text>
				</view>
				
				<view class="info-item">
					<text class="label">故障描述</text>
					<text class="value">{{ maintenanceDetail.repair_info.fault_description }}</text>
				</view>
				
				<view class="info-item">
					<text class="label">报修时间</text>
					<text class="value">{{ maintenanceDetail.repair_info.application_time }}</text>
				</view>
				
				<view class="info-item">
					<text class="label">报修状态</text>
					<text class="value">{{ maintenanceDetail.repair_info.status }}</text>
				</view>
				
				<view class="info-item" v-if="maintenanceDetail.repair_info.completion_time">
					<text class="label">完成时间</text>
					<text class="value">{{ maintenanceDetail.repair_info.completion_time }}</text>
				</view>
				
				<view class="info-item" v-if="maintenanceDetail.repair_info.repair_result">
					<text class="label">维修结果</text>
					<text class="value">{{ maintenanceDetail.repair_info.repair_result }}</text>
				</view>
			</view>
		</block>
	</view>
</template>

<script>
	import { getDeviceMaintenanceDetail } from '@/api/services.js'
	
	export default {
		data() {
			return {
				maintenanceId: null,
				maintenanceDetail: {},
				loading: true,
				loadingText: {
					contentdown: '正在加载...',
					contentrefresh: '正在加载...',
					contentnomore: '没有更多数据了'
				}
			}
		},
		onLoad(options) {
			if (options.id) {
				this.maintenanceId = options.id
				this.getMaintenanceDetail()
			} else {
				uni.showToast({
					title: '参数错误',
					icon: 'none'
				})
				setTimeout(() => {
					uni.navigateBack()
				}, 1500)
			}
		},
		methods: {
			// 获取设备维保详情
			async getMaintenanceDetail() {
				this.loading = true
				
				try {
					const response = await getDeviceMaintenanceDetail(this.maintenanceId)
					console.log('设备维保详情:', response)
					
					if (response.code === 200 && response.data) {
						this.maintenanceDetail = response.data
					} else {
						uni.showToast({
							title: response.message || '获取详情失败',
							icon: 'none'
						})
					}
				} catch (error) {
					console.error('获取设备维保详情失败', error)
					
					uni.showToast({
						title: '获取详情失败，请重试',
						icon: 'none'
					})
				} finally {
					this.loading = false
				}
			},
			
			// 获取维保类型对应的样式类
			getTypeClass(type) {
				switch (type) {
					case '维修':
						return 'type-repair'
					case '巡检':
						return 'type-inspection'
					default:
						return 'type-default'
				}
			},
			
			// 获取维保类型对应的图标
			getTypeIcon(type) {
				switch (type) {
					case '维修':
						return 'wrench'
					case '巡检':
						return 'checkbox'
					default:
						return 'info'
				}
			},
			
			// 获取维保类型对应的描述
			getTypeDesc(type) {
				switch (type) {
					case '维修':
						return '针对设备故障的修复维护'
					case '巡检':
						return '定期对设备进行检查和维护'
					default:
						return '设备维保'
				}
			},
			
			// 获取设备状态对应的样式类
			getStatusClass(status) {
				switch (status) {
					case '正常':
						return 'status-normal'
					case '故障':
						return 'status-fault'
					case '维修中':
						return 'status-repairing'
					case '报废':
						return 'status-scrapped'
					default:
						return ''
				}
			}
		}
	}
</script>

<style lang="scss">
	.detail-container {
		padding: 20rpx;
		background-color: #F8F8F8;
		min-height: 100vh;
		
		.loading {
			padding: 60rpx 0;
			text-align: center;
		}
		
		.type-card {
			background-color: #FFFFFF;
			border-radius: 12rpx;
			padding: 30rpx;
			margin-bottom: 20rpx;
			display: flex;
			align-items: center;
			box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.05);
			
			.type-icon {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				margin-right: 30rpx;
				
				&.type-repair {
					background-color: #dd524d;
				}
				
				&.type-inspection {
					background-color: #2979ff;
				}
				
				&.type-default {
					background-color: #909399;
				}
			}
			
			.type-info {
				.type-title {
					font-size: 32rpx;
					font-weight: bold;
					margin-bottom: 10rpx;
				}
				
				.type-desc {
					font-size: 26rpx;
					color: #666;
				}
			}
		}
		
		.detail-card {
			background-color: #FFFFFF;
			border-radius: 12rpx;
			padding: 30rpx;
			margin-bottom: 20rpx;
			box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.05);
			
			.card-title {
				font-size: 30rpx;
				font-weight: bold;
				margin-bottom: 20rpx;
				position: relative;
				padding-left: 20rpx;
				
				&:before {
					content: '';
					position: absolute;
					left: 0;
					top: 50%;
					transform: translateY(-50%);
					width: 6rpx;
					height: 30rpx;
					background-color: #2979ff;
					border-radius: 3rpx;
				}
			}
			
			.info-item {
				display: flex;
				margin-bottom: 20rpx;
				
				&:last-child {
					margin-bottom: 0;
				}
				
				.label {
					width: 150rpx;
					color: #666;
					font-size: 28rpx;
				}
				
				.value {
					flex: 1;
					font-size: 28rpx;
					
					&.status-normal {
						color: #19be6b;
					}
					
					&.status-fault {
						color: #dd524d;
					}
					
					&.status-repairing {
						color: #ff9900;
					}
					
					&.status-scrapped {
						color: #909399;
					}
				}
			}
		}
	}
</style> 